package com.coder.oj.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coder.oj.admin.dto.AdminDto;
import com.coder.oj.admin.mapstruct.AdminMapstruct;
import com.coder.oj.admin.query.AdminQuery;
import com.coder.oj.admin.vo.AdminVo;
import com.coder.oj.common.core.JsonPage;
import com.coder.oj.common.entity.Admin;
import com.coder.oj.common.entity.AdminRole;
import com.coder.oj.common.exception.DataException;
import com.coder.oj.common.mapper.AdminMapper;
import com.coder.oj.common.mapper.AdminRoleMapper;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminMapstruct adminMapstruct;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    /**
     * 分页查询
     * @author Bosen 2022/12/15 17:11
     **/
    public JsonPage<AdminVo> getAdminPage(AdminQuery params) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(params.getUsername()), Admin::getUsername, params.getUsername());
        queryWrapper.eq(ObjectUtils.isNotEmpty(params.getStatus()), Admin::getStatus, params.getStatus());
        IPage<Admin> page = adminMapper.selectPage(new Page<>(params.getPage(), params.getPageSize()), queryWrapper);
        List<AdminVo> collect = page.getRecords().stream().map(admin -> {
            AdminVo vo = adminMapstruct.toVO(admin);
            vo.setCreateBy(adminMapper.getAdminName(admin.getCreateBy()));
            vo.setUpdateBy(adminMapper.getAdminName(admin.getUpdateBy()));
            return vo;
        }).collect(Collectors.toList());
        return new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
    }

    /**
     * 删除管理员
     * @author Bosen 2022/12/31 0:34
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAdmin(Long id) {
        Admin admin = adminMapper.selectById(id);
        if(ObjectUtils.isEmpty(admin)) {
            return;
        }
        checkSuperAdmin(id);
        adminMapper.deleteById(id);
        adminRoleMapper.deleteByAdminId(id);
    }

    /**
     * 修改管理员
     * @author Bosen 2022/12/31 0:44
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateAdmin(AdminDto params) {
        checkSuperAdmin(params.getId());
        // 修改管理员
        adminMapper.updateById(adminMapstruct.toEntity(params));
        // 删除原有角色
        adminRoleMapper.deleteByAdminId(params.getId());
        // 新增角色
        if(!CollectionUtils.isEmpty(params.getRoleIds())) {
            for(Long roleId : params.getRoleIds()) {
                AdminRole adminRole = new AdminRole();
                adminRole.setAdminId(params.getId());
                adminRole.setRoleId(roleId);
                adminRoleMapper.insert(adminRole);
            }
        }
    }

    /**
     * 判断时候超级管理员
     * @author Bosen 2022/12/31 1:40
     */
    public boolean isAdmin(Long id) {
        Admin admin = adminMapper.selectById(id);
        return admin.getIsAdmin() == 1;
    }

    public void checkSuperAdmin(Long id) {
        if(isAdmin(id)) {
            throw new DataException("禁止操作超级管理员");
        }
    }
}
